package com.mszlu.blog.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mszlu.blog.admin.common.Result;
import com.mszlu.blog.admin.entity.Permission;
import com.mszlu.blog.admin.entity.params.PageParam;
import com.mszlu.blog.admin.entity.response.ResultList;
import com.mszlu.blog.admin.mapper.PermissionMapper;
import com.mszlu.blog.admin.service.PermissionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public Result pageList(PageParam pageParam) {

        Page<Permission> permissionPage = new Page<>(pageParam.getCurrentPage(),pageParam.getPageSize());
        LambdaQueryWrapper<Permission> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        //如果查询条件不为空,则添加查询条件到条件语句中
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(pageParam.getQueryString()),Permission::getName,pageParam.getQueryString());

        permissionMapper.selectPage(permissionPage,lambdaQueryWrapper);

        ResultList<Permission> resultList = new ResultList();
        resultList.setList(permissionPage.getRecords());
        resultList.setTotal(permissionPage.getTotal());

        return Result.success(resultList);
    }

    public Result add(Permission permission) {
        this.permissionMapper.insert(permission);
        return Result.success(null);
    }

    public Result update(Permission permission) {
        this.permissionMapper.updateById(permission);
        return Result.success(null);
    }

    public Result delete(Long id) {
        this.permissionMapper.deleteById(id);
        return Result.success(null);
    }

}
